<?php

namespace App\Logics;


use App\Models\Event;
use App\Models\EventCollect;
use App\Service\Base\Logic;

class CollectLogic extends Logic
{

    // 收藏：文章

    // 收藏文章的用户列表
    public function getCollect()
    {
        $option  = $this->option();
        $page    = $option->getData('page', 1, 'intval');
        $rows    = $option->getData('rows', 15, 'intval');
        $arti_id = $option->getData('article_id', 0, 'intval');
        $collDb  = new EventCollect();
        $result  = $collDb->bangField(['article_id'])->listPage(['article_id' => $arti_id], $page, $rows);

        // 整理用户信息
        $result['list'] = $this->listsAddUser($result['list']);

        return $result;
    }

    // 收藏列表：某个人的
    public function onLists($myUid)
    {
        $option = $this->option();
        $page   = $option->getData('page', 1, 'intval');
        $rows   = $option->getData('rows', 15, 'intval');
        $collDb = new EventCollect();
        $result = $collDb->bangField(['uid'])->listPage(['uid' => $myUid,'status'=>1], $page, $rows);

        // 整理收藏的活动
        //   以后可能会收藏文章等
        if(!empty($result['list'])){
            $aids   = array_column($result['list'], 'article_id');
            $events = (new Event())->getListInIds($aids);
            foreach($result['list'] as $key => $item){
                $result['list'][$key]['event'] = $events[$item['article_id']] ?? [];
            }
        }

        return $result;
    }

    // 收藏操作
    public function onCollect($myUid)
    {
        $option  = $this->option();
        $arti_id = $option->getData('article_id', 0, 'intval');
        if(empty($arti_id)){
            return $this->_returnFail('请指定要收藏的文章编号！');
        }

        // 执行收藏
        $collDb = new EventCollect();
        $res    = $collDb->onCollect($myUid, $arti_id);
        if($res === false){
            return $this->_returnFail($collDb->getError());
        }

        return $res;
    }

}


